/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.camachosanchez.controler.jpa;

import com.camachosanchez.bean.Almacenes;
import com.camachosanchez.bean.Neumaticos;
import com.camachosanchez.controler.jpa.exceptions.NonexistentEntityException;
import com.camachosanchez.util.JPAUtil;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityNotFoundException;
import javax.persistence.Query;

/**
 *
 * @author Rolando
 */
public class AlmacenesJpaController implements Serializable {

    public AlmacenesJpaController(EntityManagerFactory emf) {
        this.emf = emf;
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return JPAUtil.getEntityManagerFactory().createEntityManager();
    }

    public void create(Almacenes almacenes) {
        if (almacenes.getNeumaticosList() == null) {
            almacenes.setNeumaticosList(new ArrayList<Neumaticos>());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            List<Neumaticos> attachedNeumaticosList = new ArrayList<Neumaticos>();
            for (Neumaticos neumaticosListNeumaticosToAttach : almacenes.getNeumaticosList()) {
                neumaticosListNeumaticosToAttach = em.getReference(neumaticosListNeumaticosToAttach.getClass(), neumaticosListNeumaticosToAttach.getId());
                attachedNeumaticosList.add(neumaticosListNeumaticosToAttach);
            }
            almacenes.setNeumaticosList(attachedNeumaticosList);
            em.persist(almacenes);
            for (Neumaticos neumaticosListNeumaticos : almacenes.getNeumaticosList()) {
                Almacenes oldIdAlmacenOfNeumaticosListNeumaticos = neumaticosListNeumaticos.getIdAlmacen();
                neumaticosListNeumaticos.setIdAlmacen(almacenes);
                neumaticosListNeumaticos = em.merge(neumaticosListNeumaticos);
                if (oldIdAlmacenOfNeumaticosListNeumaticos != null) {
                    oldIdAlmacenOfNeumaticosListNeumaticos.getNeumaticosList().remove(neumaticosListNeumaticos);
                    oldIdAlmacenOfNeumaticosListNeumaticos = em.merge(oldIdAlmacenOfNeumaticosListNeumaticos);
                }
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Almacenes almacenes) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Almacenes persistentAlmacenes = em.find(Almacenes.class, almacenes.getId());
            List<Neumaticos> neumaticosListOld = persistentAlmacenes.getNeumaticosList();
            List<Neumaticos> neumaticosListNew = almacenes.getNeumaticosList();
            List<Neumaticos> attachedNeumaticosListNew = new ArrayList<Neumaticos>();
            for (Neumaticos neumaticosListNewNeumaticosToAttach : neumaticosListNew) {
                neumaticosListNewNeumaticosToAttach = em.getReference(neumaticosListNewNeumaticosToAttach.getClass(), neumaticosListNewNeumaticosToAttach.getId());
                attachedNeumaticosListNew.add(neumaticosListNewNeumaticosToAttach);
            }
            neumaticosListNew = attachedNeumaticosListNew;
            almacenes.setNeumaticosList(neumaticosListNew);
            almacenes = em.merge(almacenes);
            for (Neumaticos neumaticosListOldNeumaticos : neumaticosListOld) {
                if (!neumaticosListNew.contains(neumaticosListOldNeumaticos)) {
                    neumaticosListOldNeumaticos.setIdAlmacen(null);
                    neumaticosListOldNeumaticos = em.merge(neumaticosListOldNeumaticos);
                }
            }
            for (Neumaticos neumaticosListNewNeumaticos : neumaticosListNew) {
                if (!neumaticosListOld.contains(neumaticosListNewNeumaticos)) {
                    Almacenes oldIdAlmacenOfNeumaticosListNewNeumaticos = neumaticosListNewNeumaticos.getIdAlmacen();
                    neumaticosListNewNeumaticos.setIdAlmacen(almacenes);
                    neumaticosListNewNeumaticos = em.merge(neumaticosListNewNeumaticos);
                    if (oldIdAlmacenOfNeumaticosListNewNeumaticos != null && !oldIdAlmacenOfNeumaticosListNewNeumaticos.equals(almacenes)) {
                        oldIdAlmacenOfNeumaticosListNewNeumaticos.getNeumaticosList().remove(neumaticosListNewNeumaticos);
                        oldIdAlmacenOfNeumaticosListNewNeumaticos = em.merge(oldIdAlmacenOfNeumaticosListNewNeumaticos);
                    }
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = almacenes.getId();
                if (findAlmacenes(id) == null) {
                    throw new NonexistentEntityException("The almacenes with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Almacenes almacenes;
            try {
                almacenes = em.getReference(Almacenes.class, id);
                almacenes.getId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The almacenes with id " + id + " no longer exists.", enfe);
            }
            List<Neumaticos> neumaticosList = almacenes.getNeumaticosList();
            for (Neumaticos neumaticosListNeumaticos : neumaticosList) {
                neumaticosListNeumaticos.setIdAlmacen(null);
                neumaticosListNeumaticos = em.merge(neumaticosListNeumaticos);
            }
            em.remove(almacenes);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Almacenes> findAlmacenesEntities() {
        return findAlmacenesEntities(true, -1, -1);
    }

    public List<Almacenes> findAlmacenesEntities(int maxResults, int firstResult) {
        return findAlmacenesEntities(false, maxResults, firstResult);
    }

    private List<Almacenes> findAlmacenesEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select object(o) from Almacenes as o where o.estado = 1");
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Almacenes findAlmacenes(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Almacenes.class, id);
        } finally {
            em.close();
        }
    }

    public int getAlmacenesCount() {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select count(o) from Almacenes as o");
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
